package net.sevear.chaos.view
{
	import com.douban.utils.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.external.*;
	import flash.filters.*;
	import flash.net.*;
	import flash.system.*;
	
	import mx.core.UIComponent;
	import mx.effects.Tween;
	import mx.effects.easing.Linear;
	
	public class RadioCover extends UIComponent
	{
		public var dropShadow:Boolean;
		private var _clickUrl:String;
		private var picUrl:String;
		private var oldLoader:Loader;
		private var workingLoader:Loader;
		private var picture:Sprite;
		private var picMask:Shape;
		private var workingPic:Sprite;
		private var oldPic:Sprite;
		private var status:String;
		private var size:int;
		private static const DONE:String= "0";
		private static const LOADING:String = "1";
		private static const FADING:String = "2";
		
		public function RadioCover(size:int)
		{
			super();
			this.dropShadow = true;
			this._clickUrl = "";
			this.size = size;
			addEventListener(MouseEvent.CLICK, this.onClick);
			this.status = DONE;
			this.picture = new Sprite();
			this.workingPic = new Sprite();
			this.picMask = new Shape();
			addChild(this.picture);
			addChild(this.picMask);
			this.picture.addChild(this.workingPic);
			this.picMask.graphics.beginFill(0);
			this.picMask.graphics.drawRect(0, 0, this.size, this.size);
			this.picture.mask = this.picMask;
		}
		
		public function set clickUrl(url:String):void
		{
			if (url)
			{
				url = /^http:/.test(url) ? (url) : ("http://www.douban.com" + url);
				buttonMode = true;
			}
			else
			{
				buttonMode = false;
			}
			this._clickUrl = url;
			return;
		}
		
		public function get clickUrl():String
		{
			return this._clickUrl;
		}
		
		private function onClick(e:MouseEvent):void
		{
			if (this._clickUrl)
			{
				navigateToURL(new URLRequest(_clickUrl),"_blank");
			}
			
			return;
		}
		
		public function showPicture(picUrl:String):void
		{
			var context:LoaderContext;
			var filter:*;
			var url:String= picUrl;
			if (url != this.picUrl)
			{
				this.picUrl = url;
				if (this.status == LOADING)
				{
					try
					{
						if(this.workingLoader){
							this.workingLoader.close();
							this.workingLoader.unload();
						}
					}
					catch (e:Error)
					{
						trace(e.message);
					}
				}
				else
				{
					if (this.status == FADING)
					{
						this.onFadeInDone();
					}
					this.workingPic = new Sprite();
					this.workingLoader = new Loader();
					this.picture.addChild(this.workingPic);
					this.workingPic.addChild(this.workingLoader);
					this.workingLoader.addEventListener(IOErrorEvent.IO_ERROR, this.pic_ioError);
					this.workingLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onComplete);
				}
				context = new LoaderContext(true);
				this.workingLoader.load(new URLRequest(url), context);
				this.status = LOADING;
				if (this.dropShadow)
				{
					filter = new DropShadowFilter(2, 45, 0, 0.7, 4, 4, 0.7, BitmapFilterQuality.HIGH, false, false);
					filters = [filter];
				}
			}
			return;
		}
		
		private function pic_ioError(e:IOErrorEvent):void
		{
			this.status = DONE;
			dispatchEvent(e);
			return;
		}
		
		private function onComplete(e:Event):void
		{
			var bmp:Bitmap;
			var loaderInfo:LoaderInfo= e.target as LoaderInfo;
			if (this.workingLoader.content is Bitmap)
			{
				bmp = this.workingLoader.content as Bitmap;
				bmp.smoothing = true;
			}
			if (loaderInfo.height >= loaderInfo.width)
			{
				this.workingPic.height = this.size * loaderInfo.height / loaderInfo.width;
				this.workingPic.width = this.size;
				this.workingPic.x = 0;
			}
			else
			{
				this.workingPic.height = this.size;
				this.workingPic.width = this.size * loaderInfo.width / loaderInfo.height;
				this.workingPic.x = -(this.workingPic.width - this.size) / 2;
			}
			var motion:Motion = new Motion(this.workingLoader, "alpha", Linear.easeOut, 0, 1, 24);
			motion.play(this.onFadeInDone);
			this.workingLoader.removeEventListener(IOErrorEvent.IO_ERROR, pic_ioError);
			if (this.workingLoader.contentLoaderInfo)
			{
				this.workingLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, this.onComplete);
			}
			this.status = FADING;
			return;
		}
		
		private function onFadeInDone():void
		{
			if (this.status == FADING)
			{
				if (this.oldLoader)
				{
					this.oldLoader.unload();
					this.oldLoader.parent.removeChild(this.oldLoader);
				}
				if (this.oldPic)
				{
					this.picture.removeChild(this.oldPic);
					this.oldPic = null;
				}
				this.oldPic = this.workingPic;
				this.oldLoader = this.workingLoader;
				this.status = DONE;
			}
			return;
		}
	}
}